package code.rocky.sync;

import java.util.concurrent.TimeUnit;

public class SyncTest5 implements Runnable {

    private  int INIT_VALUE = 0;
    private  int TOTAL = 10;


    @Override
    public void run() {
        while (true) {
            synchronized (SyncTest5.class) { // monitorenter 有锁 不能让其他线程进来
                if (INIT_VALUE > TOTAL) {
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " INIT_VALUE: " + (INIT_VALUE++));
            }//monitorexit
        }
    }


    public static void main(String[] args) {
        SyncTest5 test5 = new SyncTest5();
        new Thread(test5, "t1").start();
        new Thread(test5, "t2").start();
        new Thread(test5, "t3").start();

    }


}


